package controllers

import (
	"context"
	"errors"
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/plat-echo/app/ops/models"
	"go.didapinche.com/foundation/plat-echo/app/ops/services"
	"go.didapinche.com/foundation/plat-echo/pkg/constants"
	"go.uber.org/zap"
	"net/http"
	"strconv"
)

type DingDingController struct {
	service services.DingDingService
	logger  *zap.Logger
}

func NewDingDingController(service services.DingDingService, logger *zap.Logger) *DingDingController {
	return &DingDingController{
		service: service,
		logger:  logger.With(zap.String("type", "controller,dingding")),
	}
}

func (ctl *DingDingController) Find(c *gin.Context) {
	info := new(models.DingDing)

	if err := c.Bind(info); err != nil {
		ctl.logger.Error("bind params error", zap.Error(err))
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}

	infos, totalCount, err := ctl.service.Find(c.Request.Context(), info)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call DingDingService Find error", zap.Error(err))
		c.String(http.StatusInternalServerError, "call DingDingService Find error:%v", err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"data":        infos,
		"total_count": totalCount,
	})
}

func (ctl *DingDingController) FindByName(c *gin.Context) {
	info := new(models.DingDing)

	if err := c.Bind(info); err != nil {
		ctl.logger.Error("bind params error", zap.Error(err))
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	info.PageSize = 20
	infos, _, err := ctl.service.Find(c.Request.Context(), info)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call DingDingService Find  error", zap.Error(err))
		c.String(http.StatusInternalServerError, "call DingDingService Find  error:%v", err)
		return
	}

	c.JSON(http.StatusOK, infos)
}

func (ctl *DingDingController) DeleteByID(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	if err != nil {
		ctl.logger.Error("convert id to integer error", zap.Error(err))
		c.String(http.StatusBadRequest, "convert id to integer error")
		return
	}

	err = ctl.service.DeleteByID(c.Request.Context(), id)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call DingDingService DeleteByID error", zap.Error(err))
		c.String(http.StatusInternalServerError, "call DingDingService DeleteByID error:%v", err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg": "delete by id ok",
	})
}

func (ctl *DingDingController) Update(c *gin.Context) {
	info := new(models.DingDing)

	if err := c.Bind(info); err != nil {
		ctl.logger.Error("bind params error", zap.Error(err))
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}

	info, err := ctl.service.Update(c.Request.Context(), info)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call DingDingService Update error", zap.Error(err))
		c.String(http.StatusInternalServerError, "call DingDingService Update error:%v", err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"data": info,
	})
}

func (ctl *DingDingController) Create(c *gin.Context) {
	info := new(models.DingDing)

	if err := c.Bind(info); err != nil {
		ctl.logger.Error("bind params error", zap.Error(err))
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}

	info, err := ctl.service.Create(c.Request.Context(), info)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			ctl.logger.Error("context canceled", zap.Error(err))
			c.Status(constants.StatusRequestCanceled)
			return
		}
		ctl.logger.Error("call DingDingService Create error", zap.Error(err))
		c.String(http.StatusInternalServerError, "call DingDingService Create error:%v", err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"data": info,
	})
}
